package com.qc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.SkillDao;
import com.qc.entity.SkillEntity;
import com.qc.entity.SkillEntityVo;

@SuppressWarnings("rawtypes")
@Repository("skillDao")
public class SkillDaoImpl implements SkillDao{
	
	@Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

	@Override
	public int getListSkillCount(String name) {

		String sql = "select count(*) from t_skill where 1 = 1 and if_del = '0' and level = '1' ";
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public List getAllSkill(String name, int pageNo, int pageSize) {

		int beginRow = (pageNo - 1)* pageSize;
        String sql = "select id,name from t_skill where 1 = 1 and if_del = '0' and level = '1' ";
        
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        
        sql += " order by created_time DESC ";
        sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
        
        return jdbcTemplate.queryForList(sql);
	}

	@Override
	public int saveSkill(SkillEntity skillEntity) {

		String sql = "insert into t_skill(name,parent,level) values(?,?,?)";
        return jdbcTemplate.update(sql,new Object[]{skillEntity.getName(),skillEntity.getParent(),skillEntity.getLevel()});
	}

	@Override
	public int deleteSkillId(long id) {

		String sql = "update t_skill set if_del = '1' where id = " +id;
        return jdbcTemplate.update(sql);
	}

	@Override
	public int updateSkillId(SkillEntity skillEntity) {

		String sql = "update t_skill set name = ? where id = ? ";
        return jdbcTemplate.update(sql,new Object[]{skillEntity.getName(),skillEntity.getId()});
	}

	@Override
	public List getAllSkillParent() {
		
		String sql = "select id,name from t_skill where 1 = 1 and if_del = '0' and level = '1' ";
		return jdbcTemplate.queryForList(sql);
	}

	@Override
	public int getListSecSkillCount(String name) {

		String sql = "select count(*) from t_skill a "
				+ " left join t_skill_tag b on b.category_id = a.id"
				+ " where 1 = 1 and b.if_del = '0' ";
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and b.name like '%"+name+"%' ";
        }
        return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public List getAllSecSkill(String name, int pageNo, int pageSize) {
		int beginRow = (pageNo - 1)* pageSize;
		String sql = "select a.id,a.name,"
				+ " b.tag_id,"
				+ " b.category_id,"
				+ " b.tag_name,"
				+ " b.tag_money,"
				+ " b.tag_content "
				+ " from t_skill a "
				+ " left join t_skill_tag b on b.category_id = a.id"
				+ " where 1 = 1 and b.if_del = '0' ";
		
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and b.name like '%"+name+"%' ";
        }
        
        sql += " order by b.created_time DESC ";
        sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
        
        return jdbcTemplate.queryForList(sql);
	}

	@Override
	public int saveSecSkill(SkillEntityVo skillEntityVo) {
		
		String sql = "insert into t_skill_tag(category_id,tag_name,tag_money,tag_content) values(?,?,?,?)";
        return jdbcTemplate.update(sql,
        		new Object[]{skillEntityVo.getCategoryId(),skillEntityVo.getTagName(),
        					skillEntityVo.getTagMoney(),skillEntityVo.getTagContent()});
	}

	@Override
	public int deleteSecSkillId(long id) {

		String sql = "update t_skill_tag set if_del = '1' where tag_id = " +id;
        return jdbcTemplate.update(sql);
	}

	@Override
	public int updateSecSkillId(SkillEntityVo skillEntityVo) {
		
		String sql = "update t_skill_tag set tag_name = ?,category_id = ?, tag_money = ? , tag_content = ? where tag_id = ? ";
        return jdbcTemplate.update(sql,
        		new Object[]{skillEntityVo.getTagName(),skillEntityVo.getCategoryId(),
        					skillEntityVo.getTagMoney(),skillEntityVo.getTagContent(),
        					skillEntityVo.getTagId()	
        		});
	}

}
